package controller

import (
	"ez/apps/erp/auto/controller"
	erp "ez/apps/erp/document"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
)

type InquiryFeedbackController struct {
	controller.InquiryFeedbackAutoController
}

func init() {
	c := &InquiryFeedbackController{}
	c.SetRouteParam("/erp", "/admin", "InquiryFeedback")
	ez.CreateApi(c.AdminGet(), c.GetAction)
	ez.CreateApi(c.AdminList(), c.ListAction)
	ez.CreateApi(c.AdminSave(), c.SaveAction)
	ez.CreateApi(c.AdminCopy(), c.CopyAction)
	ez.CreateApi(c.AdminUpdate(), c.UpdateAction)
	ez.CreateApi(c.AdminChoice(), c.ChoiceAction)
	ez.CreateApi(c.AdminDelete(), c.DeleteAction)
	ez.CreateApi(c.AdminUnDelete(), c.UnDeleteAction)
	ez.CreateApi(c.AdminDestroy(), c.DestroyAction)
	ez.CreateApi(c.AdminUpdateMany(), c.UpdateManyAction)
	ez.CreateApi(c.AdminDeleteMany(), c.DeleteManyAction)
	ez.CreateApi(c.AdminDestroyMany(), c.DestroyManyAction)
	ez.CreateApi(c.AdminApi("/InquiryFeedback/import_product"), c.ImportProductAction)
	ez.CreateApi(c.AdminApi("/InquiryFeedback/fast_buyOrder"), c.FastBuyOrderAction)
}

// func (c InquiryFeedbackController) AccessControl(session *ez.Session) { /* 在这里面重构 */ }
// func (c InquiryFeedbackController) GetAction(session *ez.Session) { /* 在这里面重构 */ }
// func (c InquiryFeedbackController) ListAction(session *ez.Session) { /* 在这里面重构 */ }

func (c InquiryFeedbackController) FastBuyOrderAction(session *ez.Session) {
	this := cc.New(session)

	inquiryFeedback, e := erp.NewInquiryFeedbackCrud(session).FindId(this.Get("id").IsInt64())
	if this.Try(e) {
		return
	}
	inquiryFeedback.LoadProduct()

	//创建询价单
	buyOrder := new(erp.BuyOrder)
	buyOrder.Sn = ""
	buyOrder.Title = inquiryFeedback.Title + " #快速采购订单"
	buyOrder.GetFactory().SetArgus(session)
	buyOrder.SetEvent(true)
	if this.Try(buyOrder.Save()) {
		return
	}
	for _, product := range inquiryFeedback.Product {
		product.GetFactory().SetArgus(session)
		buyOrderProduct := new(erp.BuyOrderProduct)
		buyOrderProduct.UnSerialize(product.Serialize())
		buyOrderProduct.BelongToId = buyOrder.Id
		buyOrderProduct.Src = inquiryFeedback.Id
		buyOrderProduct.SrcSN = inquiryFeedback.Sn
		buyOrderProduct.Id = 0
		buyOrderProduct.GetFactory().SetArgus(session)
		if this.Try(buyOrderProduct.Save()) {
			return
		}
	}
	this.ReturnSuccess("快速创建成功", "")
}

func (c InquiryFeedbackController) ImportProductAction(session *ez.Session) {
	this := cc.New(session)
	data := struct {
		Id         int64   `json:"id" bson:"id"`
		ProductIds []int64 `json:"productIds" bson:"productIds"`
		From       string  `json:"from" bson:"from"`
	}{}
	if this.Try(this.FillJson(&data)) {
		return
	}
	switch data.From {
	case "need":
		//复制请购申请中的数据
		crudNeedProduct := erp.NewNeedProductCrud(session)
		for _, id := range data.ProductIds {
			product, e := crudNeedProduct.FindId(id)
			if this.Try(e) {
				return
			}
			product.GetFactory().SetArgus(session)
			product.LoadBelongTo()

			inquiryProduct := new(erp.InquiryFeedbackProduct)
			inquiryProduct.UnSerialize(product.Serialize())
			inquiryProduct.BelongToId = data.Id
			inquiryProduct.Src = id
			inquiryProduct.SrcSN = product.BelongTo.Sn
			inquiryProduct.Id = 0
			inquiryProduct.GetFactory().SetArgus(session)
			inquiryProduct.Save()
		}
	case "inquiry":
		//复制请购申请中的数据
		crudInquiryProduct := erp.NewInquiryProductCrud(session)
		for _, id := range data.ProductIds {
			product, e := crudInquiryProduct.FindId(id)
			if this.Try(e) {
				return
			}
			product.GetFactory().SetArgus(session)
			product.LoadBelongTo()

			inquiryProduct := new(erp.InquiryFeedbackProduct)
			inquiryProduct.UnSerialize(product.Serialize())
			inquiryProduct.BelongToId = data.Id
			inquiryProduct.Src = id
			inquiryProduct.SrcSN = product.BelongTo.Sn
			inquiryProduct.Id = 0
			inquiryProduct.GetFactory().SetArgus(session)
			inquiryProduct.Save()
		}

	}
}
